package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("checkitem")
public class CheckItemController {

    @DubboReference
    CheckItemService checkitemService;

    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @PostMapping("add")
    public Result add(@RequestBody CheckItem checkItem){
        /*
        * @Date: 10:20 上午 2021/1/4
        * @param: [checkItem]
        * @return: com.itheima.health.entity.Result
        * @Description:新增检查项
        */
        log.info("[预约-检查项-添加：] - data - {}",checkItem);
        boolean flag = checkitemService.add(checkItem);
        if (flag){
            return new Result(true, MessageConst.ADD_CHECKITEM_SUCCESS);
        }else {
            return new Result(false,MessageConst.ADD_CHECKITEM_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean){
        /*
        * @Date: 11:13 上午 2021/1/4
        * @param: [queryPageBean]
        * @return: com.itheima.health.entity.Result
        * @Description: 分页查询
        */
        log.info("[预约-检查项-分页查找：] - queryPageBean - {}",queryPageBean);

        PageResult pageResult = checkitemService.findPage(queryPageBean);
        log.info("[预约-检查项-分页查找：] - pageResult - {}",pageResult);

        if (pageResult==null){
            return new Result(false,MessageConst.QUERY_CHECKITEM_FAIL);

        }
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,pageResult);
    }

    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @GetMapping("delete")
    public Result delete(Integer id){
    /*
    * @Date: 11:14 上午 2021/1/4
    * @param: [id]
    * @return: com.itheima.health.entity.Result
    * @Description: 删除检查项
    */
        log.info("[预约-检查项-删除] - id -{}",id);
        Integer count = checkitemService.delete(id);
        if (count == 0){
            return new Result(false,MessageConst.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConst.DELETE_CHECKITEM_SUCCESS);
    }

    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @GetMapping("findById")
    public Result findById(Integer id){
        /*
        * @Date: 12:19 下午 2021/1/4
        * @param: [id]
        * @return: com.itheima.health.entity.Result
        * @Description:根据ID查询检查项，可以回显数据
        */
        CheckItem checkItem = checkitemService.findById(id);
        log.info("[预约-检查项-编辑-findById] - checkItem -{}",checkItem);
        if (checkItem==null){
            return new Result(false,MessageConst.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkItem);

    }


    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @PostMapping("update")
    public Result update(@RequestBody CheckItem checkItem){
        /*
        * @Date: 2:52 下午 2021/1/4
        * @param: []
        * @return: com.itheima.health.entity.Result
        * @Description: 更改检查项数据
        */
        log.info("[预约-检查项-编辑-update] - checkItem -{}",checkItem);
        boolean flag = checkitemService.update(checkItem);
        if (flag){
            return new Result(true,MessageConst.EDIT_CHECKITEM_SUCCESS);
        }
        return new Result(false,MessageConst.EDIT_CHECKITEM_FAIL);
    }


    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @GetMapping("all")
    public Result findAll(){
        List<CheckItem> checkItemList = checkitemService.findAll();
        log.info("[预约-检查项-findAll-查询所有] - checkItemList -{}",checkItemList);

        if (checkItemList == null){
            return new Result(false,MessageConst.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkItemList);
    }
}
